<?php

class StudentHandler{

    private $regno;
    private $ini;
	private $fn;
	private $ln;
	private $gen;
	private $bdt;
	private $nic;
	private $add;
	private $cont;
	private $email;
	private $jdy;
	private $con;
	
	
	public function setRegno($reg){
	
	$this->regno=$reg;
	}

    public function setIni($init){
	
	$this->ini=$init;
	}
	
	public function setFname($fname){
	
	$this->fn=$fname;
	}

    public function setLname($lname){
	
	$this->ln=$lname;
	}
	
	 public function setGender($gender){
	
	$this->gen=$gender;
	}
	
     public function setBday($bdt){
	
	$this->bdt=$bdt;
	
	}
	
	 public function setNic($nicc){
	
	$this->nic=$nicc;
	
	}
	
	 public function setAddress($addr){
	
	$this->add=$addr;
	
	}
	
	 public function setContact($contact){
	
	$this->cont=$contact;
	
	}
	 
	 public function setEmail($emails){
	
	$this->email=$emails;
	
	}
	 
	 public function setJday($jday){
	
	$this->jdy=$jday;
	
	}
	 
	 public function setConnect($conn){
	 
	 return $this->con=$conn;
	 }
	 

//add Student information to database using this function

public function studentAdd(){
	
$sql1="insert into student(initials,f_name,l_name,gender,b_date,nic,address,tel_no,email,j_date)      values('$this->ini','$this->fn','$this->ln','$this->gen','$this->bdt','$this->nic','$this->add','$this->cont','$this->email','$this->jdy')";

$result=mysql_query("select c_fee from subject_code where sub_gr='Reg'",$this->con) or die(mysql_error());

	while($row=mysql_fetch_array($result)){

		$pfee=$row['c_fee'];
		}

		//Transaction begin
 		mysql_query("BEGIN");

		$result1=mysql_query($sql1,$this->con) or die(mysql_error());
		$reg_no=mysql_insert_id();
		//echo $reg_no;
		session_start();
		$rec=$_SESSION['user'];

	$sql2="insert into payment (reg_no,sub_gr,paid_date,p_fee,recieved_by) values('$reg_no','Reg','$this->jdy','$pfee','$rec')";
	$result2=mysql_query($sql2,$this->con) or die(mysql_error());
	$pid=mysql_insert_id();

	if (!($result1==TRUE && $result2==TRUE)){
	//when error occured transaction wilbe aborted
		mysql_query("ROLLBACK");
		echo "Rolledback Insertion";
		}
	else{
		//commit the transaction
		mysql_query("COMMIT");
		return $pid;	
		}
}

//Update student details using this function
public function studentUpdate(){
$sql="update student set initials ='$this->ini',f_name='$this->fn',l_name='$this->ln',gender='$this->gen',b_date='$this->bdt',nic='$this->nic',address= '$this->add',tel_no='$this->cont',email='$this->email',j_date='$this->jdy' where reg_no='$this->regno'"; 		
	
mysql_query($sql,$this->con) or die(mysql_error());
$msg ="Record Successfully Updated";
return $msg;	
	
}


//selct 10 rows and display on table

public function getPageSearch($startrow,$limit){

	$sql="select reg_no,f_name,l_name,gender,tel_no from student order by reg_no desc  Limit $startrow,$limit";

	$result= mysql_query($sql,$this->con)or die(mysql_error()); 
	return $result;

	}


//Delete student details
public function getDelete(){

	$sql="DELETE FROM student WHERE reg_no='$this->regno'";
	mysql_query($sql,$this->con) or die(mysql_error()) ;

	$msgd="Deleted";
	return $msgd;
}


//used to serch student details based on first name,Last name,reg no 

public function getSearch($startrow,$studsearch,$fn,$limit){ 

	if($fn=="reg")
	{
	$sql="select reg_no,f_name,l_name,gender,tel_no from student where reg_no like '$studsearch%' limit $startrow,$limit";
 	}
	else if($fn=="fn")
	{
	$sql="select reg_no,f_name,l_name,gender,tel_no from student where f_name like '$studsearch%' limit $startrow,$limit";
	}
	else
	{
	$sql="select reg_no,f_name,l_name,gender,tel_no from student where l_name like '$studsearch%'limit $startrow,$limit";
	}

$result= mysql_query($sql,$this->con)or die(mysql_error()); 
return $result;
}




public function getPaginationRow($studsearch,$fn){ 

	if($fn=="reg")
	{
	$sql="select reg_no,f_name,l_name,gender,tel_no from student where reg_no like '$studsearch%'";
	}
	else if($fn=="fn")
	{
	$sql="select reg_no,f_name,l_name,gender,tel_no from student where f_name like '$studsearch%' ";
	}
	else
	{
	$sql="select reg_no,f_name,l_name,gender,tel_no from student where l_name like '$studsearch%'";
	}

$result= mysql_query($sql,$this->con)or die(mysql_error()); 
return $result;
}




public function getReport(){

	$sql="select * from student where reg_no='$this->regno'";
	$result=mysql_query($sql,$this->con);
	return $result;
}



}
?>
